package javaCode.chapter13.set_;

import java.util.HashSet;
import java.util.Objects;

/**
 * @author 徐州城
 * @version 1.0
 */
@SuppressWarnings("all")
public class HashSetExercise01 {
    public static void main(String[] args) {
        HashSet hashSet = new HashSet();
        hashSet.add(new Employee("张三",23));
        hashSet.add(new Employee("李四",33));
        hashSet.add(new Employee("张三",23));

        System.out.println("hashSet=" + hashSet);
    }
}

/*
定义一个Employee类，该类包含：private成员属性name,age 要求：
1. 创建3个Employee 放入 HashSet中
2. 当 name和age 的值相同时，认为是相同员工，不能添加到HashSet集合中
 */

class Employee {
    private String name;
    private int age;

    public Employee(String name, int age) {
        this.name = name;
        this.age = age;
    }

//    @Override
//    public boolean equals(Object o) {
//        Employee o1 = (Employee) o;
//        return (o1.age == this.age && this.name.equals(o1.name));
//    }
//
//    //如果name 和 age的值相同，则返回相同的hash值
//    @Override
//    public int hashCode() {
//        return 200;
//    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee employee = (Employee) o;
        return age == employee.age &&
                Objects.equals(name, employee.name);
    }

    //如果name 和 age的值相同，则返回相同的hash值
    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
